<template>
  <div id="role">
    <!-- 分配权限的对话框 -->
    <el-dialog title="分配权限" :visible.sync="dialog" :before-close="closeDialog" width="35%">
      <!-- 树形控件 -->
      <el-tree ref="treeRef" :data="rightslist" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys" width="auto" style="padding: 0 60px;" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="confirmDialog">确定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
export default {
  name: 'RoleItem',
  props: {
    // 组件接收父级传递的控制弹框 打开/关闭的状态值
    dialog: {
      type: Boolean,
      default: null
    }
  },
  data() {
    return {
      // 所有权限的数据
      rightslist: [],
      // 树形控件的属性绑定对象
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 默认选中的节点Id值数组
      defKeys: [],
      // 当前即将分配权限的角色id
      roleId: ''
    }
  },
  methods: {
    // 点击取消按钮，向父级传递，让父级控制对话框关闭
    closeDialog() {
      this.defKeys = []
      this.$emit('close')
    },
    // 点击确认，处理提交表单，向父级传递
    confirmDialog() {
      const keys = [...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()]

      const idStr = keys.join(',')
      // this.$refs['treeRef'].validate((valid) => {
      //   if (valid) {
      this.$emit('role', { idStr: idStr, roleId: this.roleId })
      // }
      // })
    }
  }
}
</script>
